clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use permco permno statpers datem LTG using  $Work/XSibes.dta, clear
tsset permno datem
replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
merge 1:1 permco permno datem using $Work/EBR_new_monthly
drop if _merge==1
drop _merge
d cp* FE* 

merge 1:1 permco permno datem using $Work/Firms01_June, keepusing(permco permno datem INV BOOK MOME SIZE SI10 MOME STG2 dp)
drop if _merge==2
drop _merge
drop SIZE
rename SI10 SIZE
gstats winsor FE* cp* STG2 *LTG, cuts(1 99) by(datem) replace
* ************************************************
foreach group in BOOK SIZE INV PROF MOME {
preserve
generate portf=1+int((`group'-1)/2)
keep if portf+qruf1<.

order *, sequential    
collapse (mean) ret_mo1-ret_mo60 ret1-ret60 LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2, by(datem portf)
gen lndp=ln(dp)
drop dp

gen  Nret1Yr=1 
gen  Nret2Yr=1 
gen  Nret3Yr=1 
gen  Nret4Yr=1 
gen  Nret5Yr=1
forvalues x=1(1)60 {
	replace ret_mo`x'=ret_mo`x'-1
}	 
forvalues x=1(1)12 {
	replace Nret1Yr=Nret1Yr*(1+ret_mo`x')
}
forvalues x=13(1)24 {
	replace  Nret2Yr=Nret2Yr*(1+ret_mo`x')
}
forvalues x=25(1)36 {
	replace  Nret3Yr=Nret3Yr*(1+ret_mo`x')
}
forvalues x=37(1)48 {
	replace  Nret4Yr=Nret4Yr*(1+ret_mo`x')
}
forvalues x=49(1)60 {
	replace  Nret5Yr=Nret5Yr*(1+ret_mo`x')
}
*********************
scalar alfa= 0.97749304
gen cumNRetMo1=ln(1+ret_mo1)
gen cumNRetQr1=ln(1+ret_mo1)+ln(1+ret_mo2)+ln(1+ret_mo3)
gen cumNRetYr1=ln(Nret1Yr)
gen cumNRetYr3=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)
gen cumNRetYr5=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)+alfa^3*ln(Nret4Yr)+alfa^4*ln(Nret5Yr)
*********************
tempfile tmp
save "`tmp'", replace
*********************
keep if portf==1 | portf==5 
foreach var of varlist cumNRet* LTG L1LTG d1LLTG lndp FE* cp* d*FLTG STG2  {
	replace `var'=-`var' if portf==1 
}
foreach var of varlist cumNRet* LTG L1LTG d1LLTG lndp FE* cp* d*FLTG STG2  {
	bys datem: egen n`var'=count(`var')
}
foreach var of varlist cumNRet* LTG L1LTG d1LLTG lndp FE* cp* d*FLTG STG2  {
	replace `var'=. if n`var'<2
	drop n`var'
}
collapse (sum) cumNRet* LTG L1LTG d1LLTG lndp FE* cp* d*FLTG STG2, by(datem)
foreach var of varlist cum* LTG L1LTG d1LLTG lndp FE* cp* d*FLTG STG2 {
	replace `var'=. if `var'==0
}
generate portf=6

append using `tmp'
sort portf datem
*********************
tsset portf datem
foreach var of varlist lndp FE1-FE5 cp1-cp5 d*FLTG {
	quietly:  bys portf: egen sd=sd(`var')
	quietly: replace  `var'=`var'/sd	
	drop sd
}

eststo clear
foreach p of numlist 6 {
gen FE=1
gen cp=1
gen dFLTG=dMFLTG
eststo new_`p'_1: quietly: ivreg2 cumNRetMo1 FE cp dFLTG lndp	if portf==`p', bw(1)
drop FE cp dFLTG

gen FE=1
gen cp=1
gen dFLTG=dQFLTG
eststo new_`p'_2: quietly: ivreg2 cumNRetQr1 FE cp dFLTG lndp	if portf==`p', bw(4)
drop FE cp dFLTG

gen FE=FE1
gen cp=cp1
gen dFLTG=d1FLTG
eststo new_`p'_3: quietly: ivreg2 cumNRetYr1 FE cp dFLTG lndp	if portf==`p', bw(13)
drop FE cp dFLTG
gen FE=FE3
gen cp=cp3
gen dFLTG=d3FLTG
eststo new_`p'_4: quietly: ivreg2 cumNRetYr3 FE cp dFLTG lndp	if portf==`p', bw(37)
drop FE cp dFLTG
gen FE=FE5
gen cp=cp5
gen dFLTG=d5FLTG
eststo new_`p'_5: quietly: ivreg2 cumNRetYr5 FE cp dFLTG lndp	if portf==`p', bw(61)
drop FE cp dFLTG
}
esttab, order(FE cp dFLTG)
local file "AppendixB6_June"
if "`group'"=="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) replace  title(`group')
}
if "`group'"!="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) append  title(`group')
}
**********************************************
restore
}
